<?php

namespace App\Http\Resources\activity;

use App\Models\activity\Activity;
use App\Models\goods\Order;

use Illuminate\Http\Resources\Json\Resource;
use SimpleSoftwareIO\QrCode\Facades\QrCode;


class ActivityDetailResource extends Resource
{

    public function __construct(Activity $resource)
    {
        $this->resource = $resource;
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $uri = url('activity_sign',['id'=>$this->resource->id]);
        if (!is_file(public_path("/code/activity".config('app.version').'-'.$this->resource->id.".png"))){
            QrCode::format('png')->size(600,600)->generate($uri,public_path("/code/".config('app.version').'-'.$this->resource->id.".png"));

        }

        $applys = [];
        foreach ($this->resource->apply as $apply) {
            $applys[] = [
                'activity_id' => $apply->activity_id,
                'user_id' => $apply->user_id,
                'user_name' => $apply->user->name,
                'header_img' => $apply->user->header_img
            ];
        }

        $signs = [];
        foreach ($this->resource->sign as $sign) {
            $members[] = [
                'activity_id' => $sign->activity_id,
                'user_id' => $sign->user_id,
                'user_name' => $sign->user->name,
                'header_img' => $sign->user->header_img
            ];
        }

        $url = url("/code/".config('app.version').'-'.$this->resource->id.".png");
        return [
            'id' => $this->resource->id,
            'title' => $this->resource->title,
            'user_id' => $this->resource->user_id,
            'user_name' => $this->resource->user->name,
            'cate_id' => $this->resource->cate_id,
            'cate_name' => $this->resource->cate->name,
            'status' => $this->resource->status,
            'start_time' => $this->resource->start_time,
            'end_time' => $this->resource->end_time,
            'apply_end_time' => $this->resource->apply_end_time,
            'number' => $this->resource->number,
            'intro' => $this->resource->intro,
            'content' => $this->resource->content,
            'join_status' => $this->resource->join_status,
            'url' => $url,
            'apply' => $applys,
            'sign'  => $signs
        ];
    }
}
